module.exports = function ($scope, $modalInstance, $resource, $modal, item, toaster){

    //商客产品列表，此处根据产品编号查询指定的商客产品信息
	$scope.searchSKv4Product = function(obj){
        var para = {
            'product_code': item.code
        }
		$resource('/api/as/mc/shopproduct/findMyProductPageList', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
                obj.bean = res.data.results[0];
			}
		});
    }

    init();
    //初始化查询当前产品可售卖的全部渠道
    function init(){
        $resource('/api/as/tc/saleauth/findCanSetChannelList', {}, {}).save({'sale_category': item.sale_category}, function(res){
            if(res.errcode === 0){
                $scope.channel_list = res.data;
                var channel_codes = ',' + item.channel_list_code + ',';
                angular.forEach($scope.channel_list, function(obj){
                    if(channel_codes.indexOf(','+obj.channel_code+',') != -1){
                        obj.state = true;
                        $scope.channelFactory(obj);
                    }
                });
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }   

    //--------SKv4渠道相关操作-------
    //产品在商客微信上架(可见)
	$scope.up = function(obj) {
        if(obj.profit_level1 + obj.profit_level2 + obj.profit_level3 == 100){
            $resource('/api/as/mc/shopproduct/updateseestateone', {}, {}).save({'id': obj.id}, function(res){
                if(res.errcode === 0){
                    toaster.success({title: '', body: '上架成功。'});
                    obj.product_see_state = '1';
                } else {
                    toaster.error({title: '', body: '上架失败请重试。'});
                }
            });
        } else {
            toaster.warning({title: '', body: '三级分润不能为空'});
        }
    }
    //产品在商客微信下架(不可见)
	$scope.down = function(obj) {
		$resource('/api/as/mc/shopproduct/updateseestatezero', {}, {}).save({'id': obj.id}, function(res){
			if(res.errcode === 0){
                toaster.success({title: '', body: '下架成功。'});
                obj.product_see_state = '0';
			} else {
				toaster.error({title: '', body: '下架失败请重试。'});
			}
		});
    }
    //商客微信渠道产品设置海报
    $scope.poster = function (bean) {
        var modalInstance = $modal.open({
            template: require('../../55skwx/views/skwxPosterList.html'),
            controller: 'skwxPosterList',
            size: 'lg',
            resolve: {
                bean: function () {
                    return bean;
                }
            }
        });
        //关闭模态框刷新页面
        modalInstance.result.then(function () {
            
        });
    }
    //产品在商客微信渠道的相关配置
	$scope.productSet = function (bean) {
		var modalInstance = $modal.open({
			template: require('../../55skwx/views/skwxMyProductInfo.html'),
			controller: 'skwxMyProductInfo',
			size: 'lg',
			resolve: {
				bean: function () {
					return bean;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			
		});
	}
    //商客微信渠道设置产品分类
    $scope.setclass = function (bean) {
        var modalInstance = $modal.open({
          template: require('../../55skwx/views/skwxAddClass.html'),
          controller: 'skwxAddClass',
          size: 'lg',
          resolve: {
            product_code: function () {
              return bean.product_code;
            }
          }
        });
        //关闭模态框刷新页面
        modalInstance.result.then(function () {

        });
    }
    //-----end---------------

    $scope.updateChannel = function(info, state){
        var channel_codes = '';
        angular.forEach($scope.channel_list, function(obj){
            if(obj.channel_code == info.channel_code){
                if(!state){
                    channel_codes += obj.channel_code + ',';
                }
            } else if(obj.state){
                channel_codes += obj.channel_code + ',';
            }
        });
        var para = {
            'sale_code': item.code,
            'channel_list_code': channel_codes.substring(0,channel_codes.length-1)
        }
        $resource('/api/as/tc/saleauth/updateChannelList', {}, {}).save(para, function(res){
            if (res.errcode === 0) {
                toaster.success({title: '', body: '成功'});
                info.state = !state;
                if(!state){
                    $scope.channelFactory(info);
                }
            } else {
                toaster.error({title: '', body: '失败，请重试'});
            }
        });
    }
    
    //取消
    $scope.cancel = function () {
        $modalInstance.close();
    };



    //查询渠道配置信息工厂，根据不同的渠道编号查询不同的配置信息
    $scope.channelFactory = function(obj){
        //暂时只有商客渠道有可配置信息
        if(obj.channel_list_code == 'SKv4'){
            $scope.searchSKv4Product(obj);
        }
    };

};